package org.jeecg.modules.oa.controller.appController;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.oa.entity.OaComment;
import org.jeecg.modules.oa.entity.OaReportPlan;
import org.jeecg.modules.oa.entity.OaReportWeek;
import org.jeecg.modules.oa.entity.app.ReportWeekRequestEntity;
import org.jeecg.modules.oa.service.IOaCommentService;
import org.jeecg.modules.oa.service.IOaReportPlanService;
import org.jeecg.modules.oa.service.IOaReportWeekService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/b/app/reportWeek")
public class AppReportWeekController {

    @Autowired
    private IOaReportWeekService reportWeekService;
    @Autowired
    private IOaCommentService commentService;
    @Value("${web.imageFileUrl}")
    private String imageFileUrl;
    @Autowired
    private IOaReportPlanService reportPlanService;

    /**
     * 根据id获取周报
     *
     * @return
     */
    @PostMapping(value = "getReportWeekById")
    @ResponseBody
    public Result<?> getReportWeekById(@RequestParam(name = "id") String id,@RequestParam(name = "userId") String userId) {
        LoginUser user = SsoUtil.getLoginUser();
        if(user == null || StringUtils.isBlank(user.getId())){
            return Result.error(99,"登录失效");
        }
        OaReportWeek oaReportWeek = reportWeekService.getById(id);
        if(oaReportWeek == null){
            return Result.error("周报未找到！");
        }
        oaReportWeek = reportWeekService.processData(oaReportWeek,userId);
        ReportWeekRequestEntity reportWeekRequestEntity = new ReportWeekRequestEntity();
        BeanUtils.copyProperties(oaReportWeek,reportWeekRequestEntity);
        reportWeekRequestEntity.setComments(commentService.findAllMsgById(userId,id));
        return Result.ok(reportWeekRequestEntity);
    }

    /**
     * 新版本日志保存方法
     *
     * @param reportWeekEntity 实体对象
     * @return
     */
    @PostMapping(value = "save")
    @ResponseBody
    public Result save(ReportWeekRequestEntity reportWeekEntity) {
        LoginUser user = SsoUtil.getLoginUser();
        if(user == null || StringUtils.isBlank(user.getId())){
            return Result.error(99,"登录失效");
        }
        OaReportWeek newReportWeek = reportWeekService.findByCreateBYAndNew(reportWeekEntity.getUserId(),new Date());
        if (newReportWeek != null) {
            return Result.error("本周周报已经存在，不允许重复提交！");
        }
        Map<String, Date> weekDate = DateUtils.getWeekDate();//生成本周周一和周末的时间
        OaReportWeek reportWeek = new OaReportWeek();
        reportWeek.setCreateTime(new Date());
        reportWeek.setCreateBy(reportWeekEntity.getUserId());
        BeanUtils.copyProperties(reportWeekEntity,reportWeek);
        if(StringUtils.isNotBlank(reportWeekEntity.getNextWeekListStr())){
            List<OaReportPlan> nextPlanList = JSONObject.parseArray(reportWeekEntity.getNextWeekListStr(),OaReportPlan.class);
            reportWeek.setNextWeekList(nextPlanList);
        }
        if(StringUtils.isNotBlank(reportWeekEntity.getThisWeekListStr())){
            List<OaReportPlan> thisPlanList = JSONObject.parseArray(reportWeekEntity.getThisWeekListStr(),OaReportPlan.class);
            reportWeek.setThisWeekList(thisPlanList);
        }
        Date workDateFrom = weekDate.get("mondayDate");// 开始日期(周一)
        Date workDateTo = weekDate.get("sundayDate");// 结束日期（周日）
        reportWeek.setWorkDateFrom(workDateFrom);
        reportWeek.setWorkDateTo(workDateTo);
        reportWeekService.saveReportWeek(reportWeek);
        return Result.ok("保存成功");
    }

    /**
     * 新版本日志编辑方法
     *
     * @param reportWeekEntity 实体对象
     * @return
     */
    @PostMapping(value = "edit")
    @ResponseBody
    public Result edit(ReportWeekRequestEntity reportWeekEntity) {
        LoginUser user = SsoUtil.getLoginUser();
        if(user == null || StringUtils.isBlank(user.getId())){
            return Result.error(99,"登录失效");
        }
        OaReportWeek reportWeek = reportWeekService.getById(reportWeekEntity.getId());
        if(reportWeek == null){
            return Result.error("周报不存在,修改失败");
        }
        reportWeek.setUpdateBy(reportWeekEntity.getUserId());
        reportWeek.setUpdateTime(new Date());
        BeanUtils.copyProperties(reportWeekEntity,reportWeek);
        if(StringUtils.isNotBlank(reportWeekEntity.getThisWeekListStr())){
            List<OaReportPlan> thisPlanList = JSONObject.parseArray(reportWeekEntity.getThisWeekListStr(),OaReportPlan.class);
            reportWeek.setThisWeekList(thisPlanList);
        }else {
            reportWeek.setThisWeekList(new ArrayList<>());
        }
        if(StringUtils.isNotBlank(reportWeekEntity.getNextWeekListStr())){
            List<OaReportPlan> nextPlanList = JSONObject.parseArray(reportWeekEntity.getNextWeekListStr(),OaReportPlan.class);
            reportWeek.setNextWeekList(nextPlanList);
        }else {
            reportWeek.setNextWeekList(new ArrayList<>());
        }
        Map<String, Date> weekDate = DateUtils.getWeekDate();//生成本周周一和周末的时间
        Date workDateFrom = weekDate.get("mondayDate");// 开始日期(周一)
        Date workDateTo = weekDate.get("sundayDate");// 结束日期（周日）
        reportWeek.setWorkDateFrom(workDateFrom);
        reportWeek.setWorkDateTo(workDateTo);
        reportWeekService.editReportWeek(reportWeek);
        return Result.ok("保存成功");
    }

    @PostMapping(value = "/getThisWeekPlan")
    @ResponseBody
    public Result<List<OaReportPlan>> getThisWeekPlan(String userId) {
        LoginUser user = SsoUtil.getLoginUser();
        if(user == null || StringUtils.isBlank(user.getId())){
            return Result.error(99,"登录失效");
        }
        Result<List<OaReportPlan>> result = new Result<>();
        List<OaReportPlan> lists = reportPlanService.getThisWeekPlan(userId, new Date());
        result.setResult(lists);
        return result;
    }
}
